package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.BusPaymentOrder;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.module.bus.provider.service.hy.BusPaymentOrderService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 海鹰订单详情信息
 *
 * @author zyf
 */
@Component
public class HyOrderGetHandler implements GetInterceptor {

    @Autowired
    private TaskService taskService;

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private SysBankService bankService;


    @Autowired
    private BusHyorderSendinfoService hyorderSendinfoService;

    @Autowired
    private BusPaymentOrderService paymentOrderService;

    @Autowired
    private BusBankStatementService bankStatementService;

    @Resource
    private BusCustomService customService;

    @Resource
    private BusSalemanService salemanService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.select(BusDept.class, "deptName", "deptType");
        cq.addSelect(SysBank.class, "bankName");
        cq.createJoin(BusDept.class);
        cq.createJoin(SysBank.class);
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Integer orderState = map.getInt("orderState");
        String orderId = map.get("orderId");
        String processInstanceId = map.get("processInstanceId");
        //开票信息
        BusHyorderSendinfo sendInfo = hyorderSendinfoService.findByOrderId(orderId);
        if (FlymeUtils.isNotEmpty(sendInfo)) {
            map.put("invoiceNo", sendInfo.getInvoiceNo());
            map.put("sendInfoId", sendInfo.getSendInfoId());
            map.put("fastMailNo", sendInfo.getFastMailNo());
            map.put("fastMailName", sendInfo.getFastMailName());
            map.put("updateFastMailNo", sendInfo.getUpdateFastMailNo());
            map.put("updateFastMailName", sendInfo.getUpdateFastMailName());
            map.put("invoiceDate", DateUtils.formatDate(sendInfo.getInvoiceDate()));
        }
        if (FlymeUtils.isNotEmpty(orderId)) {
            //查询已结算明细
            List<EntityMap> paymentList = paymentService.getByOrderId(orderId);
            map.put("paymentList", paymentList);
        }

        //实时收款或者认款记录
        List<BusPaymentOrder> paymentOrders = paymentOrderService.selectByOrderId(orderId);
        if (FlymeUtils.isNotEmpty(paymentOrders)) {
            List<EntityMap> returnList = new ArrayList<>();
            for (BusPaymentOrder paymentOrder : paymentOrders) {
                EntityMap item = new EntityMap();
                item.put("returnDate", DateUtils.formatDate(paymentOrder.getTakeDate()));
                item.put("returnAmount", paymentOrder.getTakeAmount());
                item.put("useAmount", paymentOrder.getUseAmount());
                SysBank sysBank = bankService.getById(paymentOrder.getBankId());
                if (FlymeUtils.isNotEmpty(sysBank)) {
                    item.put("returnBank", sysBank.getBankName());
                    item.put("returnBankNo", sysBank.getBankNo());
                }
                String bankStatementId = paymentOrder.getBankStatementId();
                if (FlymeUtils.isNotEmpty(bankStatementId)) {
                    BusBankStatement bankStatement = bankStatementService.getById(bankStatementId);
                    if (FlymeUtils.isNotEmpty(bankStatement)) {
                        item.put("bankStatementId", bankStatement.getBankStatementId());
                        item.put("payBankNo", bankStatement.getBankNo());
                        item.put("customName", bankStatement.getCustomName());
                        item.put("remark", bankStatement.getRemark());
                    }
                } else {
                    String customId = paymentOrder.getCustomId();
                    if (FlymeUtils.isNotEmpty(customId)) {
                        BusCustom custom = customService.getById(customId);
                        if (FlymeUtils.isNotEmpty(custom)) {
                            item.put("customName", custom.getCustomName());
                        }
                    }
                }
                returnList.add(item);
            }
            map.put("returnList", returnList);
        }

        Long saleManIdD = map.getLong("saleManIdD");
        Long saleManIdC = map.getLong("saleManIdC");
        Long saleManIdE = map.getLong("saleManIdE");
        if (FlymeUtils.isNotEmpty(saleManIdD)) {
            BusSaleman saleManD = salemanService.getById(saleManIdD);
            map.put("saleManNameD", "商务经理:" + saleManD.getSaleManName());
        }
        if (FlymeUtils.isNotEmpty(saleManIdC)) {
            BusSaleman saleManC = salemanService.getById(saleManIdC);
            map.put("saleManNameC", "  片区服务商" + saleManC.getSaleManName());
        }
        if (FlymeUtils.isNotEmpty(saleManIdE) && !saleManIdE.equals(1L)) {
            BusSaleman saleManE = salemanService.getById(saleManIdE);
            map.put("saleManNameE", "  商务总负责人:" + saleManE.getSaleManName());
        }
        //审批状态
        if (FlymeUtils.isNotEmpty(processInstanceId) && orderState.equals(1)) {
            //查询当前审批节点
            Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
            if (FlymeUtils.isNotEmpty(task)) {
                map.put("taskName", task.getName());
                map.put("taskId", task.getId());
                map.put("taskDefinitionKey", task.getTaskDefinitionKey());
            }
        }
    }
}
